package com.dmwork.service.interaction.service.impl;

import com.dmwork.common.base.result.R;
import com.dmwork.common.base.result.ResultCodeEnum;
import com.dmwork.service.base.exception.DmworkException;
import com.dmwork.service.interaction.entity.WorkComment;
import com.dmwork.service.interaction.entity.WorkCommentLike;
import com.dmwork.service.interaction.entity.vo.CommentLikeMessageVo;
import com.dmwork.service.interaction.feign.CustomerFeignService;
import com.dmwork.service.interaction.mapper.WorkCommentLikeMapper;
import com.dmwork.service.interaction.service.WorkCommentLikeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dmwork.service.interaction.service.WorkCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 作品评论的点赞表 服务实现类
 * </p>
 *
 * @author 李天
 * @since 2021-04-26
 */
@Service
public class WorkCommentLikeServiceImpl extends ServiceImpl<WorkCommentLikeMapper, WorkCommentLike> implements WorkCommentLikeService {

    @Autowired
    private CustomerFeignService customerFeignService;
    @Autowired
    private WorkCommentService workCommentService;

    @Override
    public List<CommentLikeMessageVo> getCommentLikeMessageList(List<String> workCommentLikeIdList) {
        //获取作品部分点赞记录
        List<WorkCommentLike> workCommentLikeList = baseMapper.selectBatchIds(workCommentLikeIdList);

        //获取所有点赞的用户的用户名和头像以及被点赞的评论记录
        List<String> customerIdList=new ArrayList<>(); //点赞的用户的id
        List<String> commentIdList=new ArrayList<>(); //被点赞的评论的id
        for (WorkCommentLike workCommentLike : workCommentLikeList) {
            customerIdList.add(workCommentLike.getCustomerId());
            commentIdList.add(workCommentLike.getCommentId());
        }
        R customerBriefInfoListResult = customerFeignService.getBriefInfoList(customerIdList);
        if (!customerBriefInfoListResult.getSuccess()) {
            throw new DmworkException(ResultCodeEnum.SELECT_CUSTOMER_INFO_ERROR);
        }
        HashMap<String, List<String>> customerBriefInfoMap = (HashMap<String, List<String>>) customerBriefInfoListResult.getData().get("result");
        List<WorkComment> workCommentList = workCommentService.listByIds(commentIdList);

        //数据组装
        List<CommentLikeMessageVo> commentLikeMessageVoList=new ArrayList<>();
        for (WorkCommentLike workCommentLike : workCommentLikeList) {
            CommentLikeMessageVo commentLikeMessageVo = new CommentLikeMessageVo();
            commentLikeMessageVo.setLikeId(workCommentLike.getId());
            commentLikeMessageVo.setCustomerId(workCommentLike.getCustomerId());
            commentLikeMessageVo.setCustomerName(customerBriefInfoMap.get(workCommentLike.getCustomerId()).get(0));
            commentLikeMessageVo.setCustomerAvatar(customerBriefInfoMap.get(workCommentLike.getCustomerId()).get(1));
            commentLikeMessageVo.setCommentId(workCommentLike.getCommentId());
            commentLikeMessageVo.setType(0);
            commentLikeMessageVo.setGmtCreate(workCommentLike.getGmtCreate());
            for (WorkComment workComment : workCommentList) {
                if (commentLikeMessageVo.getCommentId().equals(workComment.getId())) {
                    commentLikeMessageVo.setCommentContent(workComment.getContent());
                    commentLikeMessageVo.setWorkId(workComment.getWorkId());
                    break;
                }
            }
            commentLikeMessageVoList.add(commentLikeMessageVo);
        }
        return commentLikeMessageVoList;
    }
}
